//do something config the frame
//所谓的frame 只是保存些配置信息。
//依赖 jquery.ui.notice.js
(function( $, undefined ) {
	
	var Frame=function(element, options){
		
	}, escape = /["\\\x00-\x1f\x7f-\x9f]/g,
		meta = {
			'\b': '\\b',
			'\t': '\\t',
			'\n': '\\n',
			'\f': '\\f',
			'\r': '\\r',
			'"' : '\\"',
			'\\': '\\\\'
		},notice=$("body").notice().hide(),
	hasOwn = Object.prototype.hasOwnProperty;;
	
	Frame.prototype = {
	   constructor: Frame,
	   getNotice:function(){
		   return notice;
	   }
	};
	$.fn.frame=function(){
		return new Frame();
	};
	$.fn.frame.defaults = {};
	$.fn.frame.Constructor = Frame;
	
	// copy from the form.js
	$.fn.clearForm = function() {
	    return this.each(function() {
	        $('input,select,textarea', this).clearFields();
	    });
	};
	/**
	 * Clears the selected form elements.
	 *  copy from the form.js
	 */
	$.fn.clearFields = $.fn.clearInputs = function() {
	    return this.each(function() {
	        var t = this.type, tag = this.tagName.toLowerCase();
	        if (t == 'text' || t == 'password' || tag == 'textarea')
	            this.value = '';
	        else if (t == 'checkbox' || t == 'radio')
	            this.checked = false;
	        else if (tag == 'select')
	            this.selectedIndex = -1;
	    });
	};
	/**
	 * Resets the form data.  Causes all form elements to be reset to their original value.
	 * copy from the form.js
	 */
	$.fn.resetForm = function() {
	    return this.each(function() {
	        // guard against an input with the name of 'reset'
	        // note that IE reports the reset function as an 'object'
	        if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType))
	            this.reset();
	    });
	};
	
	$.quoteString = function (str) {
		if (str.match(escape)) {
			return '"' + str.replace(escape, function (a) {
				var c = meta[a];
				if (typeof c === 'string') {
					return c;
				}
				c = a.charCodeAt();
				return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
			}) + '"';
		}
		return '"' + str + '"';
	};
	//将json对象转化为json字符串
	$.toJSON = function (o) {
		if (o === null) {
			return 'null';
		}

		var pairs, k, name, val,type = $.type(o);

		if (type === 'undefined') {
			return undefined;
		}

		// Also covers instantiated Number and Boolean objects,
		// which are typeof 'object' but thanks to $.type, we
		// catch them here. I don't know whether it is right
		// or wrong that instantiated primitives are not
		// exported to JSON as an {"object":..}.
		// We choose this path because that's what the browsers did.
		if (type === 'number' || type === 'boolean') {
			return String(o);
		}
		if (type === 'string') {
			return $.quoteString(o);
		}
		if (typeof o.toJSON === 'function') {
			return $.toJSON(o.toJSON());
		}
		if (type === 'date') {
			var month = o.getUTCMonth() + 1,
				day = o.getUTCDate(),
				year = o.getUTCFullYear(),
				hours = o.getUTCHours(),
				minutes = o.getUTCMinutes(),
				seconds = o.getUTCSeconds(),
				milli = o.getUTCMilliseconds();

			if (month < 10) {
				month = '0' + month;
			}
			if (day < 10) {
				day = '0' + day;
			}
			if (hours < 10) {
				hours = '0' + hours;
			}
			if (minutes < 10) {
				minutes = '0' + minutes;
			}
			if (seconds < 10) {
				seconds = '0' + seconds;
			}
			if (milli < 100) {
				milli = '0' + milli;
			}
			if (milli < 10) {
				milli = '0' + milli;
			}
			return '"' + year + '-' + month + '-' + day + 'T' +
				hours + ':' + minutes + ':' + seconds +
				'.' + milli + 'Z"';
		}

		pairs = [];

		if ($.isArray(o)) {
			for (k = 0; k < o.length; k=k+1) {
				pairs.push($.toJSON(o[k]) || 'null');
			}
			return '[' + pairs.join(',') + ']';
		}

		// Any other object (plain object, RegExp, ..)
		// Need to do typeof instead of $.type, because we also
		// want to catch non-plain objects.
		if (typeof o === 'object') {
			for (k in o) {
				// Only include own properties,
				// Filter out inherited prototypes
				if (hasOwn.call(o, k)) {
					// Keys must be numerical or string. Skip others
					type = typeof k;
					if (type === 'number') {
						name = '"' + k + '"';
					} else if (type === 'string') {
						name = $.quoteString(k);
					} else {
						continue;
					}
					type = typeof o[k];

					// Invalid values like these return undefined
					// from toJSON, however those object members
					// shouldn't be included in the JSON string at all.
					if (type !== 'function' && type !== 'undefined') {
						val = $.toJSON(o[k]);
						pairs.push(name + ':' + val);
					}
				}
			}
			return '{' + pairs.join(',') + '}';
		}
	};
	//将form序列化为一个json对象
	$.fn.serializeJson=function(){
		var serializeObj={},
		    array=this.serializeArray(),
		    str=this.serialize();
		
		$(array).each(function(){
			if(serializeObj[this.name]){
				if($.isArray(serializeObj[this.name])){
				   serializeObj[this.name].push(this.value);
				}else{
				   serializeObj[this.name]=[serializeObj[this.name],this.value];
				}
			}else{
			    serializeObj[this.name]=this.value;
			}
		});
		return serializeObj;
	};
	
	//配置全局异步请求的提示消息
	(function(){
		
		$(document).ajaxSuccess(function(a,b,c,fb){
			if(fb.msg&&fb.msgType){
				notice.show().setMessage($.fn.getMSG(fb.msg,fb.replacer));
			}
	    });
	})();
	//以下是日期函数
	
	
}( jQuery ) );
